﻿using System.Collections.Generic;
using UnityEngine;

namespace MyResKit
{
    public class ResLoader
    {		
        /// <summary>
        /// 持有的
        /// </summary>
        private List<Res> mResList = new List<Res>();

        public T Load<T>(string assetName, string assetBundleName = null) where T : Object
        {
            var loadedRes = mResList.Find(loadedAsset => loadedAsset.Name == assetName);

            if (loadedRes != null)
            {
                return loadedRes as T;
            }

            loadedRes = ResMgr.GetRes(assetName, assetBundleName);

            mResList.Add(loadedRes);

            return loadedRes.Asset as T;

        }

        public void UnloadAll()
        {
            foreach (var asset in mResList)
            {
                asset.Release();
            }

            mResList.Clear();
            mResList = null;
        }
    }
}